package com.rory.tiny.modules.pms.controller;


import com.rory.tiny.common.api.CommonResult;
import com.rory.tiny.common.enums.RedisKeyPrefixEnum;
import com.rory.tiny.common.service.RedisService;
import com.rory.tiny.modules.pms.dto.request.Lv2CodeRequestDTO;
import com.rory.tiny.modules.pms.dto.response.Lv2CodeResponseDTO;
import com.rory.tiny.modules.pms.service.PmsLv2CodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rory
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/pms/pmsLv2Code")
public class PmsLv2CodeController {

	@Resource
	private PmsLv2CodeService pmsLv2CodeService;

	@Resource
	private RedisService redisService;

	/**
	 * 查询二级代码
	 *
	 * @param lv2CodeRequestDTO
	 * @return {@link CommonResult< Lv2CodeResponseDTO>}
	 * @author zhangzedi
	 * @date 2022/11/29 11:05
	 */
	@ApiOperation("查询二级代码")
	@PostMapping("/query")
	public CommonResult<List<Lv2CodeResponseDTO>> queryLv2CodeByType(@RequestBody Lv2CodeRequestDTO lv2CodeRequestDTO) {
		List<Lv2CodeResponseDTO> responseDTOList;
		//先从缓存中获取
		Object obj = redisService.get(RedisKeyPrefixEnum.REGION_LIST.getPrefix());
		if (obj != null) {
			responseDTOList = (List<Lv2CodeResponseDTO>) obj;
		} else {
			responseDTOList = pmsLv2CodeService.queryLv2CodeByType(lv2CodeRequestDTO);
			redisService.set(RedisKeyPrefixEnum.REGION_LIST.getPrefix(), responseDTOList);
		}
		return CommonResult.success(responseDTOList);
	}
}

